<form id="add-form" class="form-horizontal" method="POST" action="{:url('gsave')}">
    <div class="form-group">
        <label class="control-label col-xs-12 col-sm-2">功能名称:</label>
        <div class="col-xs-12 col-sm-8">
            <input type="text" name="name" value="{$data['name']|default=''}" class="form-control"/>
        </div>
    </div>
    <div class="form-group">
        <label class="control-label col-xs-12 col-sm-2">所属模块:</label>
        <div class="col-xs-12 col-sm-8">
            <select class="form-control" name="model_id" id="model_id" >
                 <option value="0">请选择</option>
                {volist name="models" id="vo"}  

                <option value="{$vo.id}" {if condition="((!empty($data)) AND $data['model_id'] eq $vo['id']) OR ($vo['id'] eq $model_id)"}selected{/if}>{$vo.name}</option>
               {/volist} 
            </select>
        </div>
    </div>
    <div class="form-group">
        <label class="control-label col-xs-12 col-sm-2">功能清单:</label>
    </div>
    <div class="my-copy-body">
        <div class="my-body" id="child_list">
        {notempty name="child"}
          {volist name="child" id="vo"}    
            <div class="form-group child">
                <label class="control-label col-xs-12 col-sm-2"></label>
                <div class="col-xs-12 col-sm-8"  style="background-color: #fff;padding-top: 15px;">
                    <div class="form-group">
                        <label class="control-label col-xs-12 col-sm-2">名称:</label>
                        <div class="col-xs-12 col-sm-8">
                            <input type="text" class="form-control" name="cname" value="{$vo.cname}"  />
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-xs-12 col-sm-2">应用:</label>
                        <div class="col-xs-12 col-sm-8">
                            {php}$vo['type_id']=explode(',',$vo['type_id']);{/php}
                           {volist name="type" id="ch"}
                            <label style="font-weight: normal;margin-right: 15px;cursor: pointer;">{$ch.name}<input style="position: relative;top: 2px;" name="type_id[]" {if condition="(!empty($vo)) AND in_array($ch['id'],$vo['type_id'])"}checked{/if} value="{$ch.id}" type="checkbox">
                            </label>
                           {/volist}
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-xs-12 col-sm-2">说明:</label>
                        <div class="col-xs-12 col-sm-8">
                            <textarea type="text" name="des" class="form-control">{$vo['des']|default=''}</textarea>
                        </div>
                    </div>
                    <input type="hidden" name="cid" value="{$vo['id']|default=''}" />
                </div>

            </div>
         {/volist}
       {else/}
          <div class="form-group child">
                <label class="control-label col-xs-12 col-sm-2"></label>
                <div class="col-xs-12 col-sm-8"  style="background-color: #fff;padding-top: 15px;">
                    <div class="form-group">
                        <label class="control-label col-xs-12 col-sm-2">名称:</label>
                        <div class="col-xs-12 col-sm-8">
                            <input type="text" class="form-control" name="cname"   />
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-xs-12 col-sm-2">应用:</label>
                        <div class="col-xs-12 col-sm-8">
                           {volist name="type" id="ch"}   
                            <label style="font-weight: normal;margin-right: 15px;cursor: pointer;">{$ch.name}<input style="position: relative;top: 2px;" name="type_id[]" 
                             value="{$ch.id}" type="checkbox">
                            </label>
                          {/volist}   
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-xs-12 col-sm-2">说明:</label>
                        <div class="col-xs-12 col-sm-8">
                            <textarea type="text" name="des" class="form-control"></textarea>
                        </div>
                    </div>
                    <input type="hidden" name="cid" value="" />
                </div>

            </div>
       {/notempty}

        </div>
    </div>
    <div style="text-align: center;margin-bottom: 15px;">
        <button type="button" class="btn btn-success copy">添加更多</button>
        <button type="button" class="btn btn-success copy-del">删除一个</button>
    </div>
    <div id="my-copy-box"></div>
    <div class="form-group">
        <label class="control-label col-xs-12 col-sm-2"></label>
        <div class="col-xs-12 col-sm-8">
            <input type="hidden" name="id" value="{$data['id']|default=''}" />
            <button type="submit" id="submit" class="btn btn-success">{:__('OK')}</button>
        </div>
    </div>
</form>

<div id="template" style="display: none;">
     <div class="form-group child">
                <label class="control-label col-xs-12 col-sm-2"></label>
                <div class="col-xs-12 col-sm-8"  style="background-color: #fff;padding-top: 15px;">
                    <div class="form-group">
                        <label class="control-label col-xs-12 col-sm-2">名称:</label>
                        <div class="col-xs-12 col-sm-8">
                            <input type="text" class="form-control" name="cname"   />
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-xs-12 col-sm-2">应用:</label>
                        <div class="col-xs-12 col-sm-8">
                           {volist name="type" id="ch"}   
                            <label style="font-weight: normal;margin-right: 15px;cursor: pointer;">{$ch.name}<input style="position: relative;top: 2px;" name="type_id[]" 
                             value="{$ch.id}" type="checkbox">
                            </label>
                          {/volist}   
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-xs-12 col-sm-2">说明:</label>
                        <div class="col-xs-12 col-sm-8">
                            <textarea type="text" name="des" class="form-control"></textarea>
                        </div>
                    </div>
                    <input type="hidden" name="cid" value="" />
                </div>

            </div>
</div>

<script>
    $(function(){
        $(".copy").click(function(){
           var child_dom=$("#template").html();
           $("#child_list").append(child_dom);
        });
         $(".copy-del").click(function(){
            $("#child_list").find(".child").eq(-1).remove();
        });
        $("#submit").click(function(){
             var items = new Array();
             var cheklist=$("#child_list").find(".child");
              for (var i = 0; i < cheklist.length; i++) {
                    var child_box=$(cheklist[i]);
                    var cname=child_box.find("input[name=cname]").val();
                    var des=child_box.find("[name=des]").val();
                    var cid=child_box.find("input[name=cid]").val();
                    var type_id='';

                    $.each(child_box.find('input[name="type_id[]"]:checked'),function(){
                        type_id+=$(this).val()+',';
                    });
                  var item= {id:cid,cname: cname, des: des,type_id:type_id};
                 items.push(item);
            }
           var child=JSON.stringify( items );

           var name=$("input[name=name]").val();
           var model_id=$("#model_id").val();
           var id=$("input[name=id]").val();
           $.ajax({
                    type:'POST',
                    data:{
                        child:child,
                        id:id,
                        name:name,
                        model_id:model_id,
                    },
                    url:"{:url('gsave')}",
                    traditional: true,
                    success:function(data){                            
                          if(data.code==1){
                            alert(data.msg);
                            window.location.href=data.url;
                          }else{
                            alert(data.msg);
                          }
                    }
            });   
            return false;        
        });

    })
</script>